package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/1/24 14:35
 * @apiNote
 */

public class Test00541ReverseStr {

    public static void main(String[] args) {
        String s = "abcdefg";
        String result = reverseStr(s,2);
        System.out.println(result);
    }

    public static String reverseStr(String s, int k) {
        int len = s.length();
        char[] arr =  s.toCharArray();
        for(int i = 0; i < len; i+= 2*k){
            reverse(arr, i, Math.min(i + k - 1, len - 1));
        }
        return new String(arr);
    }

    public static void reverse(char[] chars, int start, int end) {
        while (start < end) {
            char temp = chars[start];
            chars[start] = chars[end];
            chars[end] = temp;
            start++;
            end--;
        }
    }
}
